package net.galacticwar.replayserver.admin;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class LoginFrame extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JTextField userName;
	private JLabel userNameLbl;
	private JLabel serverAdressLbl;
	private AbstractAction Login;
	private JLabel versionlabel;
	private AbstractAction quitAction;
	private JPasswordField password;
	private JButton quitButton;
	private JButton loginButton;
	private JTextField serverAdress;
	private JLabel jLabel1;
	
	private LoginCallback loginCallback;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				LoginFrame inst = new LoginFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public LoginFrame() {
		super();
		initGUI();
	}
	
	public void setLoginCallback(LoginCallback loginCallback) {
		this.loginCallback = loginCallback;
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Live Replay Server login");
			{
				userName = new JTextField();
				userName.setAction(getLogin());
			}
			{
				userNameLbl = new JLabel();
				userNameLbl.setText("Login name");
				userNameLbl.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				serverAdress = new JTextField();
				serverAdress.setText("localhost:6881");
				serverAdress.setAction(getLogin());
			}
			{
				serverAdressLbl = new JLabel();
				serverAdressLbl.setText("Server Adress");
				serverAdressLbl.setHorizontalTextPosition(SwingConstants.RIGHT);
				serverAdressLbl.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				loginButton = new JButton();
				loginButton.setText("Login");
				loginButton.setAction(getLogin());
			}
			{
				quitButton = new JButton();
				quitButton.setText("Quit");
				quitButton.setAction(getQuitAction());
			}
			{
				jLabel1 = new JLabel();
				jLabel1.setText("Password");
				jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(18, 18)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(serverAdress, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(serverAdressLbl, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(userName, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
				    .addComponent(userNameLbl, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(getPassword(), GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(loginButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
				    .addComponent(quitButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, Short.MAX_VALUE)
				.addComponent(getVersionlabel(), GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap(37, 37)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(loginButton, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
				        .addGap(17))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
				        .addGap(14))
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(userNameLbl, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
				        .addGap(14))
				    .addComponent(serverAdressLbl, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addComponent(quitButton, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
				        .addGap(39))
				    .addComponent(getPassword(), GroupLayout.Alignment.LEADING, 0, 112, Short.MAX_VALUE)
				    .addGroup(thisLayout.createSequentialGroup()
				        .addGap(0, 0, Short.MAX_VALUE)
				        .addComponent(userName, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE))
				    .addComponent(serverAdress, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGap(0, 18, Short.MAX_VALUE)
				        .addComponent(getVersionlabel(), GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)))
				.addContainerGap());
			pack();
			this.setSize(261, 178);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	* This method should return an instance of this class which does 
	* NOT initialize it's GUI elements. This method is ONLY required by
	* Jigloo if the superclass of this class is abstract or non-public. It 
	* is not needed in any other situation.
	 */
	public static Object getGUIBuilderInstance() {
		return new LoginFrame(Boolean.FALSE);
	}
	
	/**
	 * This constructor is used by the getGUIBuilderInstance method to
	 * provide an instance of this class which has not had it's GUI elements
	 * initialized (ie, initGUI is not called in this constructor).
	 */
	public LoginFrame(Boolean initGUI) {
		super();
	}
	
	public JButton getLoginButton() {
		return loginButton;
	}
	
	public JButton getQuitButton() {
		return quitButton;
	}
	
	private AbstractAction getLogin() {
		if(Login == null) {
			Login = new AbstractAction("Login", null) {
				public void actionPerformed(ActionEvent evt) {
					String[] splitted = serverAdress.getText().split(":");
					String adress = splitted[0];
					int port = Integer.parseInt(splitted[1]);
					
					if (loginCallback!=null) {
						loginCallback.connect(adress, port, userName.getText(), password.getPassword());
					}
				}
			};
		}
		return Login;
	}
	
	private JPasswordField getPassword() {
		if(password == null) {
			password = new JPasswordField();
		}
		return password;
	}
	
	private AbstractAction getQuitAction() {
		if(quitAction == null) {
			quitAction = new AbstractAction("Quit", null) {
				public void actionPerformed(ActionEvent evt) {
					if (loginCallback!=null) {
						loginCallback.disconnect();
					}
				}
			};
		}
		return quitAction;
	}
	
	private JLabel getVersionlabel() {
		if(versionlabel == null) {
			versionlabel = new JLabel();
			versionlabel.setText("Version " + AdminTools.VERSION);
			versionlabel.setHorizontalTextPosition(SwingConstants.RIGHT);
			versionlabel.setHorizontalAlignment(SwingConstants.RIGHT);
		}
		return versionlabel;
	}

}
